package com.jjc.android.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import com.jjc.android.GlobalManager
import com.jjc.android.R
import com.jjc.android.adapter.SimpleFragmentPagerAdapter
import com.jjc.android.base.BaseActivity
import com.jjc.android.dialog.BindDeviceDialog
import com.jjc.android.fragment.HomeFragment
import com.jjc.android.fragment.MineFragment
import com.jjc.android.view.onClick
import com.lxj.xpopup.XPopup

class MainActivity : BaseActivity() {

    companion object{
        fun start(context:Context){
            val intent = Intent(context, MainActivity::class.java)
            context.startActivity(intent)
        }
    }

    override val layoutId: Int = R.layout.activity_main
    private val mineTab: TextView by lazy { findViewById<TextView>(R.id.mineTab) }
    private val centerTab: TextView by lazy { findViewById<TextView>(R.id.centerTab) }
    private val homeTab: TextView by lazy { findViewById<TextView>(R.id.homeTab) }
    private val viewPager: ViewPager by lazy { findViewById<ViewPager>(R.id.viewPager) }
    private val takePhotoBtn: TextView by lazy { findViewById<TextView>(R.id.takePhotoBtn) }
    private val camera2Btn: TextView by lazy { findViewById<TextView>(R.id.camera2Btn) }

    override fun initView(savedInstanceState: Bundle?) {
        takePhotoBtn.setOnClickListener {
            CameraActivity.start(this)
        }
        camera2Btn.setOnClickListener {
            startActivity(Intent(this,ProbeActivity::class.java))
        }
        val fragment = HomeFragment.newInstance()
        centerTab.onClick {
            fragment.currentDevice.let {device->
                if(device==null){
                    XPopup.Builder(this)
                        .isDestroyOnDismiss(true)
                        .dismissOnBackPressed(false)
                        .dismissOnTouchOutside(false)
                        .asCustom(BindDeviceDialog(this))
                        .show()
                }else{
                    DetectionActivity.start(this, testerId = fragment.currentTester?.id, deviceSn = device.bhmohipntdlpc61m)
                }
            }


        }
        viewPager.adapter = SimpleFragmentPagerAdapter(
            supportFragmentManager,
            listOf(fragment,MineFragment.newInstance())
        )
        homeTab.isActivated = true
        mineTab.isActivated = false
        homeTab.setOnClickListener {
            if(viewPager.currentItem == 0) return@setOnClickListener
            homeTab.isActivated = true
            mineTab.isActivated = false
            viewPager.currentItem = 0
        }
        mineTab.setOnClickListener {
            if(viewPager.currentItem == 1) return@setOnClickListener
            homeTab.isActivated = false
            mineTab.isActivated = true
            viewPager.currentItem = 1
        }
    }

}